<template>
  <div class="login">
    <div class="title">
      <p>密码登录</p>
    </div>
    <div class="zh">
      <input type="text" v-model="userLogin.phone" placeholder="手机/用户名" />
      <input type="password" v-model="userLogin.pass" placeholder="密码" />
    </div>
    <div class="dl">
      <input type="button" @click="login" value="登录" />
    </div>
    <div class="mm">
      <span>手机号登录</span>
      <span>忘记密码</span>
    </div>
  </div>
</template>

<script>
import { user_login } from "../utils/api";
export default {
  data() {
    return {
      userLogin: {
        phone: "",
        pass: "",
      },
    };
  },
  methods: {
    login() {
      // console.log(this.userLogin.phone, this.userLogin.pass);

      if (/^1[3|5|7]\d{9}$/.test(this.userLogin.phone) == false) {
        alert("请输入正确的手机号");
      } else if (/^\d{6}$/.test(this.userLogin.pass) == false) {
        alert("请输入正确的密码");
      } else {
        user_login(this.userLogin).then((res) => {
          // console.log(res.data);
          if (res.data.code == 200) {
            localStorage.setItem("token", res.data.token);
            localStorage.setItem("userinfo", JSON.stringify(res.data.userinfo));
            this.$router.push("/");
          } else {
            alert("登录失败！");
          }
        });
      }
    },
  },
};
</script>

<style lang='scss' scoped>
.login {
  padding: 20px;
  .title {
    font-size: 30px;
  }
  .zh {
    margin-top: 20px;
    input {
      height: 40px;
      width: 100%;
      margin-top: 30px;
      outline: none;
      border: none;
      border-bottom: 1px solid #999;
    }
  }
  .dl {
    input {
      margin-top: 40px;
      width: 100%;
      height: 40px;
      outline: none;
      border: none;
      border-radius: 20px;
    }
  }
  .mm {
    margin-top: 20px;
    display: flex;
    justify-content: space-between;
    span {
      color: aqua;
    }
  }
}
</style>